import yaml
import requests
import time
import json
from loguru import logger
import os
import textract
import chardet

config_path = 'config/config.yaml'


def get_config(key: str) -> dict:
    with open(config_path, 'r', encoding='utf-8') as f:
        data = yaml.safe_load(f.read())
    return data.get(key)


def set_nested_value(d, keys, value):
    """
    更新嵌套字典的指定路径值。
    d: 目标字典
    keys: 由点分割的路径列表（如 ['database', 'host']）
    value: 要设置的新值
    """
    for key in keys[:-1]:  # 遍历路径，直到最后一个键
        d = d.get(key, {})  # 确保字典存在，如果没有则创建
    d[keys[-1]] = value  # 最后一个键设置为新值


def update_config(key: str, value: str):
    with open(config_path, 'r', encoding='utf-8') as f:
        data = yaml.safe_load(f.read())
    keys = key.split('.')  # 将路径按点分割成列表
    set_nested_value(data, keys, value)

    with open(config_path, 'w', encoding='utf-8') as file:
        yaml.dump(data, file, default_flow_style=False, allow_unicode=True)


def get_proxy(redis_conn):
    authKey = 'A68CD182'
    password = 'D4DED4D8D54B'
    headers = {
        "User-Agent": "Mozilla/5.0"
    }
    proxy_url = 'https://share.proxy.qg.net/pool?key=A68CD182&num=3&area=&isp=&format=json&seq=&distinct=false&pool=1'
    redis_key = f'spider-proxy_list'
    proxy_dict = redis_conn.lpop(redis_key)

    if not proxy_dict or proxy_dict == b'null':
        while True:
            res = requests.get(proxy_url, headers=headers, timeout=20).json()
            if 'SUCCESS' not in res['code']:
                logger.info(res)
                time.sleep(1)
            else:
                break
        if res:
            for one in res['data']:
                proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
                    "user": authKey,
                    "password": password,
                    "server": one['server'],
                }
                proxy = {'http': proxyUrl, 'https': proxyUrl}
                redis_conn.rpush(redis_key, json.dumps(proxy))
                logger.info(f'代理已经更新{proxy}')
                return proxy
    else:
        proxy_dict = json.loads(proxy_dict)
    redis_conn.close()
    return proxy_dict


def extract_text_from_file(file_path: str) -> str:
    """
    使用 textract 提取文件文本，支持 pdf, docx, txt, pptx, xlsx 等格式。
    """
    try:
        text = textract.process(file_path).decode('utf-8')
        return text
    except Exception as e:
        raise RuntimeError(f"无法提取文本: {e}")


if __name__ == '__main__':
    print(extract_text_from_file("data/dianli/test.docx"))
